<?php

/**
 * Specify test object. Tests table.
 */
class test
{
    /**
     * Test id.
     */
    var $id;

    /**
     * Test description.
     */
    var $description;

    /**
     * Test name.
     */
    var $name;

    /**
     * Test type.
     */
    var $type;

    /**
     * Test author.
     */
    var $author;

    /**
     * Questions count.
     */
    var $questions_count;

    /**
     * Exam mode.
     */
    var $is_exam_mode;

    /**
     * Random answers.
     */
    var $is_random_answers;

    /**
     * Use time limit.
     */
    var $is_time_limit;

    /**
     * Time limit.
     */
    var $time_limit;

    /**
     * Possible back.
     */
    var $is_back;

    /**
     * Show score.
     */
    var $is_show_score;

    /**
     * Show results message.
     */
    var $is_show_results_message;

    /**
     * Next when right.
     */
    var $is_next_when_right;

    /**
     * Show reference.
     */
    var $is_show_reference;

    /**
     * Response on right.
     */
    var $is_response_on_right;

    /**
     * Response on wrong.
     */
    var $is_response_on_wrong;

    /**
     * Text of right message.
     */
    var $text_of_right_message;

    /**
     * Text of wrong message.
     */
    var $text_of_wrong_message;

    /**
     * Show explanation.
     */
    var $is_show_explanation;

    /**
     * Conclusion type.
     */
    var $concl_type;

    /**
     * Stat total.
     */
    var $is_stat_total;

    /**
     * Stat current.
     */
    var $is_stat_current;

    /**
     * Stat rights.
     */
    var $is_stat_rights;

    /**
     * Stat time.
     */
    var $is_stat_time;

    /**
     * Media storage.
     */
    var $media_storage;

    /**
     * Sort by theme.
     */
    var $is_sort_by_theme;

    /**
     * Max count.
     */
    var $max_count;
	
	/**
     * CSS.
     */
    var $css;
}

/**
 * Get test from RecordSet.
 *
 * @param $result RecordSet
 *
 * @return test class
 */
function get_test_from_result($result)
{
    $test = new test();

    $test->author =
        $result->fields['test_author'];
    $test->concl_type =
        $result->fields['test_concl_type'];
    $test->description =
        $result->fields['test_description'];
    $test->id =
        $result->fields['id'];
    $test->is_back =
        $result->fields['test_is_back'];
    $test->is_exam_mode =
        $result->fields['test_is_exam_mode'];
    $test->is_next_when_right =
        $result->fields['test_is_next_when_right'];
    $test->is_random_answers =
        $result->fields['test_is_random_answers'];
    $test->is_response_on_right =
        $result->fields['test_is_response_on_right'];
    $test->is_response_on_wrong =
        $result->fields['test_is_response_on_wrong'];
    $test->is_show_explanation =
        $result->fields['test_is_show_explanation'];
    $test->is_show_reference =
        $result->fields['test_is_show_reference'];
    $test->is_show_results_message =
        $result->fields['test_is_show_results_message'];
    $test->is_show_score =
        $result->fields['test_is_show_score'];
    $test->is_sort_by_theme =
        $result->fields['test_is_sort_by_theme'];
    $test->is_stat_current =
        $result->fields['test_is_stat_current'];
    $test->is_stat_rights =
        $result->fields['test_is_stat_rights'];
    $test->is_stat_time =
        $result->fields['test_is_stat_time'];
    $test->is_stat_total =
        $result->fields['test_is_stat_total'];
    $test->is_time_limit =
        $result->fields['test_is_time_limit'];
    $test->max_count =
        $result->fields['test_max_count'];
	$test->css =
        $result->fields['test_css'];
    $test->media_storage =
        $result->fields['test_media_storage'];
    $test->name =
        $result->fields['test_name'];
    $test->questions_count =
        $result->fields['test_questions_count'];
    $test->text_of_right_message =
        $result->fields['test_text_of_right_message'];
    $test->text_of_wrong_message =
        $result->fields['test_text_of_wrong_message'];
    $test->time_limit =
        $result->fields['test_time_limit'];
    $test->type =
        $result->fields['test_type'];

    return $test;
}

/**
 * Add test.
 *
 * @param $p_test test class
 * @param $p_id int test id. If test id is 0 - add test with next test id.
 *
 * @return RecordSet or false
 */
function add_test($p_test, $p_id = 0)
{
    $t_id = db_prepare_int($p_id);

    $t_test_description = db_prepare_string($p_test->description);
    $t_test_name = db_prepare_string($p_test->name);
    $t_test_type = db_prepare_int($p_test->type);
    $t_test_author = db_prepare_string($p_test->author);
    $t_test_questions_count =
        db_prepare_int($p_test->questions_count);
    $t_test_is_exam_mode = db_prepare_int($p_test->is_exam_mode);
    $t_test_is_random_answers =
        db_prepare_int($p_test->is_random_answers);
    $t_test_is_time_limit = db_prepare_int($p_test->is_time_limit);
    $t_test_time_limit = db_prepare_string($p_test->time_limit);
    $t_test_is_back = db_prepare_int($p_test->is_back);
    $t_test_is_show_score = db_prepare_int($p_test->is_show_score);
    $t_test_is_show_results_message =
        db_prepare_int($p_test->is_show_results_message);
    $t_test_is_next_when_right =
        db_prepare_int($p_test->is_next_when_right);
    $t_test_is_show_reference =
        db_prepare_int($p_test->is_show_reference);
    $t_test_is_response_on_right =
        db_prepare_int($p_test->is_response_on_right);
    $t_test_is_response_on_wrong =
        db_prepare_int($p_test->is_response_on_wrong);
    $t_test_text_of_right_message =
        db_prepare_string($p_test->text_of_right_message);
    $t_test_text_of_wrong_message =
        db_prepare_string($p_test->text_of_wrong_message);
    $t_test_is_show_explanation =
        db_prepare_int($p_test->is_show_explanation);
    $t_test_concl_type = db_prepare_int($p_test->concl_type);
    $t_test_is_stat_total = db_prepare_int($p_test->is_stat_total);
    $t_test_is_stat_current = db_prepare_int($p_test->is_stat_current);
    $t_test_is_stat_rights = db_prepare_int($p_test->is_stat_rights);
    $t_test_is_stat_time = db_prepare_int($p_test->is_stat_time);
    $t_test_media_storage =
        db_prepare_string($p_test->media_storage);
    $t_test_is_sort_by_theme =
        db_prepare_int($p_test->is_sort_by_theme);
    $t_test_max_count = db_prepare_int($p_test->max_count);
	$t_test_css =
        db_prepare_string($p_test->css);

    if ($t_id == 0)
    {
        $insert = "";
        $values = "";
    }
    else
    {
        $insert = "id,";
        $values = "'$t_id',";
    }

    $query =  'INSERT INTO '.DB_TABLE_TESTS.'(
            '.$insert.'
            `test_description`,
            `test_name`,
            `test_type`,
            `test_author`,
            `test_questions_count`,
            `test_is_exam_mode`,
            `test_is_random_answers`,
            `test_is_time_limit`,
            `test_time_limit`,
            `test_is_back`,
            `test_is_show_score`,
            `test_is_show_results_message`,
            `test_is_next_when_right`,
            `test_is_show_reference`,
            `test_is_response_on_right`,
            `test_is_response_on_wrong`,
            `test_text_of_right_message`,
            `test_text_of_wrong_message`,
            `test_is_show_explanation`,
            `test_concl_type`,
            `test_is_stat_total`,
            `test_is_stat_current`,
            `test_is_stat_rights`,
            `test_is_stat_time`,
            `test_media_storage`,
            `test_is_sort_by_theme`,
            `test_max_count`,
			`test_css`)
        VALUES (
            '.$values.
            $t_test_description.',
            '.$t_test_name.',
            '.$t_test_type.',
            '.$t_test_author.',
            '.$t_test_questions_count.',
            '.$t_test_is_exam_mode.',
            '.$t_test_is_random_answers.',
            '.$t_test_is_time_limit.',
            '.$t_test_time_limit.',
            '.$t_test_is_back.',
            '.$t_test_is_show_score.',
            '.$t_test_is_show_results_message.',
            '.$t_test_is_next_when_right.',
            '.$t_test_is_show_reference.',
            '.$t_test_is_response_on_right.',
            '.$t_test_is_response_on_wrong.',
            '.$t_test_text_of_right_message.',
            '.$t_test_text_of_wrong_message.',
            '.$t_test_is_show_explanation.',
            '.$t_test_concl_type.',
            '.$t_test_is_stat_total.',
            '.$t_test_is_stat_current.',
            '.$t_test_is_stat_rights.',
            '.$t_test_is_stat_time.',
            '.$t_test_media_storage.',
            '.$t_test_is_sort_by_theme.',
			'.$t_test_max_count.',
            '.$t_test_css.')';

    return db_exec($query);
}

/**
 * Get tests.
 *
 * @param $p_sort_field string sort field
 * @param $p_sort_order string sort order: ASC or DESC
 * @param $p_page int page number
 * @param $p_count int items count on the page
 * @param $p_filter test_filter object
 *
 * @return array tests array
 */
function get_tests(  $p_sort_field = "id",
            $p_sort_order = "ASC",
            $p_page = 1,
            $p_count = 0,
            $p_filter = NULL
            )
{
    $t_sort_field = db_escape_string($p_sort_field);
    $t_sort_order = db_prepare_sort_order($p_sort_order);
    $t_page = db_prepare_int($p_page);
    $t_count = db_prepare_int($p_count);

    $query = 'SELECT * FROM '.DB_TABLE_TESTS;
    $tmp =  ($p_filter != NULL) ? $p_filter->query() : '';
    if ($tmp != '')
    {
        $query .= ' WHERE '.$tmp;
    }
    if ($t_sort_field == "")
        $t_sort_field = "id";
    $query .= " ORDER BY `$t_sort_field` $t_sort_order";

    if ($p_count != 0)
    {
        $limit = ($t_page-1)*$t_count;
        $query .= " LIMIT  $limit, $t_count";
    }

    return db_query($query);
}

/**
 * Get tests from test id array.
 *
 * @param $p_id_array test id array
 *
 * @return array tests array
 */
function get_tests_from_array($p_id_array)
{

    $tmp = "";
    $array = array_values($p_id_array);
    $size = sizeof($array);

    if ($size == 0)
        return NULL;

    for($i=0; $i<$size-1; $i++)
        $tmp .=  db_prepare_int($array[$i]).", ";
    $tmp .= db_prepare_int($array[$size-1]);

    $query = 'SELECT * FROM '.DB_TABLE_TESTS.
        ' WHERE `id` IN('.$tmp.')
        ORDER BY `id` ASC';

    return db_query($query);
}

/**
 * Get test id by name.
 *
 * @param $p_name test name
 *
 * @return int test id. 0- test does not exist
 */
function get_test_id_by_name($p_name)
{
    $t_name = db_prepare_string($p_name);

    $query =  'SELECT `id`
        FROM '.DB_TABLE_TESTS.
        ' WHERE `test_name` = '.$t_name;

    $result = db_query($query);

    return isset($result[0]['id']) ? $result[0]['id'] : 0;
}

/**
 * Get tests count.
 *
 * @param $p_filter test_filter object
 *
 * @return int tests count
 */
function get_tests_count($p_filter)
{
    $tmp =  ($p_filter != NULL) ? $p_filter->query() : '';

    return db_count(DB_TABLE_TESTS, $tmp);

}

/**
 * Get tests count for section id.
 *
 * @param $p_id int section id
 *
 * @return int tests count
 */
function get_tests_count_for_section_id($p_id)
{
    $t_id = db_prepare_int($p_id);

    return db_count(DB_TABLE_SECTION_TEST, '`section_id` = '.$t_id);
}

/**
 * Get test.
 *
 * @param $p_id int test id
 *
 * @return test class
 */
function get_test($p_id)
{
    global $adodb;

    $t_id = db_prepare_int($p_id);

    $query = 'SELECT * FROM '.DB_TABLE_TESTS.
         ' WHERE id = '.$t_id;
    $result = $adodb->Execute($query);

    $test = new test();

    if (!$result->EOF)
    {
        $test = get_test_from_result($result);
    }

    return $test;
}

/**
 * Get value from array. If value does not exists - return empty string.
 *
 * @param $items array
 * @param $key string key
 *
 * @return mixed if key exist, or '' if key does not exist
 */
function get_item_data($items, $key)
{
    return array_key_exists($key, $items) && isset($key, $items) ? $items[$key] : '';
}

/**
 * Import test.
 *
 * @param $file string xml test file
 * @param $id int test id
 *
 * @return int test id
 */
function import_test($file, $id = 0)
{
    global $adodb;

    $items = get_xml_data(CFG_TESTS_DIR.$file);

    // Test info.
    $test = new test();
    $test->description                      = get_item_data($items, "TEST/INFO/DESCRIPTION_HTML");
    $test->name                             = get_item_data($items, "TEST/INFO/NAME");
    $test->type                             = get_item_data($items, "TEST/INFO/TESTTYPE");
    $test->author                           = get_item_data($items, "TEST/INFO/AUTHOR");
    $test->questions_count                  = get_item_data($items, "TEST/INFO/QUESTIONSCOUNT");
    $test->is_exam_mode                     = get_item_data($items, "TEST/INFO/ISEXAMMODE") == "TRUE" ? 1 : 0;
    $test->is_random_answers                = get_item_data($items, "TEST/INFO/ISRANDOMANSWERS") == "TRUE" ? 1 : 0;
    $test->is_sort_by_theme                 = get_item_data($items, "TEST/INFO/ISSORTBYTHEME") == "TRUE" ? 1 : 0;
    $test->is_time_limit                    = get_item_data($items, "TEST/INFO/ISTIMELIMIT") == "TRUE" ? 1 : 0;
    $test->time_limit                       = get_item_data($items, "TEST/INFO/TIMELIMIT");
    $test->is_back                          = get_item_data($items, "TEST/INFO/ISBACK") == "TRUE" ? 1 : 0;
    $test->is_show_score                    = get_item_data($items, "TEST/INFO/ISSHOWSCORE") == "TRUE" ? 1 : 0;
    $test->is_show_results_message          = get_item_data($items, "TEST/INFO/ISSHOWRESULTSMESSAGE") == "TRUE" ? 1 : 0;
    $test->is_next_when_right               = get_item_data($items, "TEST/INFO/ISNEXTWHENRIGHT") == "TRUE" ? 1 : 0;
    $test->is_show_reference                = get_item_data($items, "TEST/INFO/ISSHOWREFERENCE") == "TRUE" ? 1 : 0;
    $test->is_response_on_right             = get_item_data($items, "TEST/INFO/ISRESPONSEONRIGHT") == "TRUE" ? 1 : 0;
    $test->is_response_on_wrong             = get_item_data($items, "TEST/INFO/ISRESPONSEONWRONG") == "TRUE" ? 1 : 0;
    $test->text_of_right_message            = get_item_data($items, "TEST/INFO/TEXTOFRIGHTMESSAGE");
    $test->text_of_wrong_message            = get_item_data($items, "TEST/INFO/TEXTOFWRONGMESSAGE");
    $test->is_show_explanation              = get_item_data($items, "TEST/INFO/ISSHOWEXPLANATION") == "TRUE" ? 1 : 0;
    $test->media_storage                    = get_item_data($items, "TEST/INFO/MEDIASTORAGE");
    $test->max_count                        = get_item_data($items, "TEST/INFO/LIMITCOUNT");
    $test->concl_type                       = get_item_data($items, "TEST/INFO/CONCLUSIONTYPE");
	$test->css                       		= get_item_data($items, "TEST/INFO/CSS");
    $theme_quantity                         = get_item_data($items, "TEST/THEMES/QUANTITY");
    $resume_count                           = get_item_data($items, "TEST/RESUME/RESUMECOUNT");

    // Statistics
    $test->is_stat_total                    = get_item_data($items, "TEST/STATISTICS/TOTAL") == "TRUE" ? 1 : 0;
    $test->is_stat_current                  = get_item_data($items, "TEST/STATISTICS/CURRENT") == "TRUE" ? 1 : 0;
    $test->is_stat_rights                   = get_item_data($items, "TEST/STATISTICS/RIGHTS") == "TRUE" ? 1 : 0;
    $test->is_stat_time                     = get_item_data($items, "TEST/STATISTICS/TIME") == "TRUE" ? 1 : 0;

    add_test($test, $id);

    if ($adodb->ErrorMsg() != '')
    {
        return 0;
    }

    // Test id
    if ($id == 0)
    {
        $test_id                            = $adodb->Insert_ID();
    }
    else
    {
        $test_id                            = $id;
    }
    $test->id                               = $test_id;

    // Hints
    $hints_count                            = get_item_data($items, "TEST/HINTS/QUANTITY");

    $hints = array();

    for ($i = 0; $i < $hints_count; $i++)
    {
        $hint = new hint();

        $hint->test                         = $test_id;
        $hint->number                       = $i;
        $hint->title                        = get_item_data($items, "TEST/HINTS/HINT_$i/CAPTION");
        $hint->text                         = get_item_data($items, "TEST/HINTS/HINT_$i/TEXT");
        $hint->html_text                    = get_item_data($items, "TEST/HINTS/HINT_$i/TEXT_HTML");

        add_hint($hint);

        $hint->id                           = $adodb->Insert_ID();

        $hints[]                            = $hint;
    }


    // Themes
    $themes_count                           = get_item_data($items, "TEST/THEMES/QUANTITY");

    $themes = array();

    for ($i = 0; $i < $themes_count; $i++)
    {
        $theme                              = new theme();

        $theme->test                        = $test_id;
        $theme->number                      = $i;
        $theme->caption                     = get_item_data($items, "TEST/THEMES/THEME_$i/CAPTION");
        $theme->show_in_results             = get_item_data($items, "TEST/THEMES/THEME_$i/SHOWINRESULTS");
        $theme->numexam                     = get_item_data($items, "TEST/THEMES/THEME_$i/NUMEXAM");
        $theme->conclusions_count           = get_item_data($items, "TEST/CONCLUSIONS/THEME_$i/CONCLUSIONSCOUNT");

        add_theme($theme);
        $theme->id                          = $adodb->Insert_ID();

        $themes[]                           = $theme;
    }

    // Conclusions
    foreach($themes as $theme)
    {
        for ($i = 0; $i < $theme->conclusions_count; $i++)
        {
            $theme_number                   = $theme->number;
            $conclusion                     = new conclusion();
            $conclusion->theme              = $theme->id;
            $conclusion->number             = $i;
            $conclusion->top                = get_item_data($items, "TEST/CONCLUSIONS/THEME_".$theme_number."/CONCLUSION_".$i."/TOP");
            $conclusion->low                = get_item_data($items, "TEST/CONCLUSIONS/THEME_".$theme_number."/CONCLUSION_".$i."/LOW");
            $conclusion->text               = get_item_data($items, "TEST/CONCLUSIONS/THEME_".$theme_number."/CONCLUSION_".$i."/TEXT");

            add_conclusion($conclusion);
            $conclusion->id                 = $adodb->Insert_ID();
        }
    }

    // Resume
    for ($i = 0; $i < $resume_count; $i++)
    {
        $resume = new resume();

        $resume->test                       = $test_id;
        $resume->number                     = $i;
        $resume->top                        = get_item_data($items, "TEST/RESUME/RESUME_$i/TOP");
        $resume->low                        = get_item_data($items, "TEST/RESUME/RESUME_$i/LOW");
        $resume->text                       = get_item_data($items, "TEST/RESUME/RESUME_$i/TEXT");

        add_resume($resume);
        $resume->id                         = $adodb->Insert_ID();
    }

    // Questions
    $questions_count                        = get_item_data($items, "TEST/INFO/QUESTIONSCOUNT");
    for ($i = 0; $i < $questions_count; $i++)
    {
        $question = new question();
        $question->number = $i;
        $question->text                     = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/TEXT");
        $question->text_bin                 = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/TEXT_BINARY");
        $question->text_html                = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/TEXT_HTML");
        $question->time                     = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/TIME");
        $question->answ_quantity            = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/AQUANTITY");
        $question->type                     = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/TYPE");
        $question->explanation              = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/EXPLANATION");
        $question->css                      = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/CSS");

        // TODO: Загрузить image.
        $question->picture_url              = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/MMFILE");

        // Get theme id.
        $theme_number                       = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/THEMEID");
        $theme                              = $themes[$theme_number];
        $question->theme_id                 = $theme->id;


        $question->weight                   = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/WEIGHT");

        // Get hint id.
        if ($hints_count > 0)
        {
            $question->hint                 = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/HINT");
        }
        else
        {
            $question->hint                 = -1;
        }

        $question->etalon                   = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/ETALON/TEXT");
        $question->is_case_sensetive        = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/ETALON/ISCASESENSETIVE") == "TRUE" ? 1 : 0;
        $question->etalon_score             = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/ETALON/SCORE");

        add_question($question);
        $question->id                       = $adodb->Insert_ID();


        // Answers
        $answers_count                      = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/AQUANTITY");

        for ($answers_index =0; $answers_index < $answers_count; $answers_index++)
        {
            $answer                         = new answer();
            $answer->question               = $question->id;
            $answer->number                 = $answers_index;
            $answer->text                   = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/ANSWERS/ANSWER_$answers_index/TEXT");
            $answer->score                  = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/ANSWERS/ANSWER_$answers_index/SCORE");
            $answer->right                  = (get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/ANSWERS/ANSWER_$answers_index/ISRIGHT") == "TRUE") ? 1 : 0;
            $answer->text_html              = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/ANSWERS/ANSWER_$answers_index/TEXT_HTML");
            $answer->text_bin               = isset($items["TEST/QUESTIONS/QUESTION_$i/ANSWERS/ANSWER_$answers_index/TEXT_BINARY"]) ? get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/ANSWERS/ANSWER_$answers_index/TEXT_BINARY") : '';
            $answer->corresp                = get_item_data($items, "TEST/QUESTIONS/QUESTION_$i/ANSWERS/ANSWER_$answers_index/CORRESPONDENCE");
            $answer->nextq                  = -1;

            add_answer($answer);
        }
    }

    return $test_id;
}


/**
 * Delete test.
 *
 * @param $p_id int test id
 * @param $must bool must remove test
 *
 * @return bool result
 */
function delete_test($p_id, $must = FALSE)
{
    global $adodb;

    $t_id = db_prepare_int($p_id);

    $count = get_sections_count_for_test_id($t_id);

    if (($count == 0) || $must)
    {
        $query =  'DELETE FROM '.DB_TABLE_TESTS.
            ' WHERE `id` = '.$t_id;
        $adodb->Execute($query);
        delete_hints_for_test_id($t_id);
        delete_resume_for_test_id($t_id);
        delete_themes_for_test_id($t_id);

        return TRUE;
    }

    return FALSE;
}

/**
 * Delete tests.
 *
 * @param id_array int array tests id
 */
function delete_tests($id_array)
{
    $tmp = TRUE;
    foreach($id_array as $id)
    {
        $result =  delete_test($id);
        $tmp = $tmp && $result;
    }

    return $tmp;
}

/**
 * Update test.
 *
 * @param $p_id int test id
 * @param $file string xml test file
 */
function update_test($p_id, $file)
{
    delete_test($p_id, TRUE);
    import_test($file, $p_id);
}

/**
 * Edit test.
 *
 * @param $p_id int test id
 * @param $test test class
 *
 * @return RecordSet or false
 */
function edit_test($p_id, $test)
{
    $t_id = db_prepare_int($p_id);

    $t_test_description = db_prepare_string($test->description);
    $t_test_name = db_prepare_string($test->name);
    $t_test_type = db_prepare_int($test->type);
    $t_test_author = db_prepare_string($test->author);
    $t_test_questions_count =
        db_prepare_int($test->questions_count);
    $t_test_is_exam_mode =
        db_prepare_int($test->is_exam_mode);
    $t_test_is_random_answers =
        db_prepare_int($test->is_random_answers);
    $t_test_is_time_limit =
        db_prepare_int($test->is_time_limit);
    $t_test_time_limit = db_prepare_string($test->time_limit);
    $t_test_is_back = db_prepare_int($test->is_back);
    $t_test_is_show_score =
        db_prepare_int($test->is_show_score);
    $t_test_is_show_results_message =
        db_prepare_int($test->is_show_results_message);
    $t_test_is_next_when_right =
        db_prepare_int($test->is_next_when_right);
    $t_test_is_show_reference =
        db_prepare_int($test->is_show_reference);
    $t_test_is_response_on_right =
        db_prepare_int($test->is_response_on_right);
    $t_test_is_response_on_wrong =
        db_prepare_int($test->is_response_on_wrong);
    $t_test_text_of_right_message =
        db_prepare_string($test->text_of_right_message);
    $t_test_text_of_wrong_message =
        db_prepare_string($test->text_of_wrong_message);
    $t_test_is_show_explanation =
        db_prepare_int($test->is_show_explanation);
    $t_test_concl_type = db_prepare_int($test->concl_type);
    $t_test_is_stat_total =
        db_prepare_int($test->is_stat_total);
    $t_test_is_stat_current =
        db_prepare_int($test->is_stat_current);
    $t_test_is_stat_rights =
        db_prepare_int($test->is_stat_rights);
    $t_test_is_stat_time =
        db_prepare_int($test->is_stat_time);
    $t_test_media_storage =
        db_prepare_string($test->media_storage);
    $t_test_is_sort_by_theme =
        db_prepare_int($test->is_sort_by_theme);
    $t_test_max_count = db_prepare_int($test->max_count);
	$t_test_css =
        db_prepare_string($test->css);

    $query = 'UPDATE '.DB_TABLE_TESTS.
        ' SET `test_description` = '.$t_test_description.',
        `test_name` = '.$t_test_name.',
        `test_type` = '.$t_test_type.',
        `test_author` = '.$t_test_author.',
        `test_questions_count` = '.$t_test_questions_count.',
        `test_is_exam_mode` = '.$t_test_is_exam_mode.',
        `test_is_random_answers` = '.$t_test_is_random_answers.',
        `test_is_time_limit` = '.$t_test_is_time_limit.',
        `test_time_limit` = '.$t_test_time_limit.',
        `test_is_back` = '.$t_test_is_back.',
        `test_is_show_score` = '.$t_test_is_show_score.',
        `test_is_show_results_message` =
            '.$t_test_is_show_results_message.',
        `test_is_next_when_right` = '.$t_test_is_next_when_right.',
        `test_is_show_reference` = '.$t_test_is_show_reference.',
        `test_is_response_on_right` =
            '.$t_test_is_response_on_right.',
        `test_is_response_on_wrong` =
            '.$t_test_is_response_on_wrong.',
        `test_text_of_right_message` =
            '.$t_test_text_of_right_message.',
        `test_text_of_wrong_message` =
            '.$t_test_text_of_wrong_message.',
        `test_is_show_explanation` =
            '.$t_test_is_show_explanation.',
        `test_concl_type` = '.$t_test_concl_type.',
        `test_is_stat_total` = '.$t_test_is_stat_total.',
        `test_is_stat_current` = '.$t_test_is_stat_current.',
        `test_is_stat_rights` = '.$t_test_is_stat_rights.',
        `test_is_stat_time` = '.$t_test_is_stat_time.',
        `test_media_storage` = '.$t_test_media_storage.',
        `test_is_sort_by_theme` = '.$t_test_is_sort_by_theme.',
        `test_max_count` = '.$t_test_max_count.',
		`test_css` = '.$t_test_css.'
        WHERE `id` = '.$t_id;

    return db_exec($query);
}

/**
 * Get tests for user id.
 *
 * @param $p_user_id int user id
 *
 * @return array tests array
 */
function get_tests_for_user_id($p_user_id)
{

    $t_user_id = db_prepare_int($p_user_id);

    $query =  'SELECT DISTINCT '.
            DB_TABLE_TESTS.'.id, '.DB_TABLE_TESTS.'.test_description, '.DB_TABLE_TESTS.'.test_name 
        FROM '.DB_TABLE_TESTS.', '.
            DB_TABLE_GROUP_SECTION.', '.
            DB_TABLE_SECTION_TEST.', '.
            DB_TABLE_USER.
        ' WHERE '.DB_TABLE_USER.'.`user_group_id` = '.DB_TABLE_GROUP_SECTION.'.`group_id`
        AND '.DB_TABLE_GROUP_SECTION.'.`section_id` = '.DB_TABLE_SECTION_TEST.'.`section_id`
        AND     '.DB_TABLE_SECTION_TEST.'.`test_id` = '.DB_TABLE_TESTS.'.`id`
        AND '.DB_TABLE_SECTION_TEST.'.`test_is_hidden` = 0
        AND     '.DB_TABLE_USER.'.`id` = '.$t_user_id.
        ' ORDER BY '.DB_TABLE_TESTS.'.`test_name` ASC';

    return db_query($query);
}


/**
 * Get unhidden tests for section id.
 *
 * @param $p_section_id int section id
 *
 * @return array tests array
 */
function get_unhidden_tests_for_section_id($p_section_id)
{

    $t_section_id = db_prepare_int($p_section_id);

    $query = 'SELECT '.DB_TABLE_TESTS.'.*
          FROM  '.DB_TABLE_TESTS.', '.
            DB_TABLE_SECTION_TEST.
        ' WHERE '.DB_TABLE_SECTION_TEST.'.`section_id` = '.$t_section_id.
        ' AND   '.DB_TABLE_SECTION_TEST.'.`test_is_hidden` = 0
        AND '.DB_TABLE_SECTION_TEST.'.`test_id` = '.DB_TABLE_TESTS.'.`id`
        ORDER BY '.DB_TABLE_TESTS.'.`test_name` ASC';

    return db_query($query);
}

